package com.ftg.learn.day12.datastruct;

import java.util.Arrays;

/**
 * 学习一下hash
 *
 * @author 布衣
 */
public class LearnHash {

    /**
     * hash表
     * 链表数组
     */
    public Node[] table;

    int defaultSize = 16;

    public void put(String key, Object value) {
        if (table == null) {
            table = new Node[defaultSize];
        }
        int hash = key.hashCode();

        int index = hash % table.length;

        if (table[index] == null) {
            table[index] = new Node(key, value);
        } else {
            Node current = table[index];
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node(key, value);
        }
    }

    private class Node {
        private String key;
        private Object value;
        private Node next;

        public Node() {
        }

        public Node(String key, Object value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String toString() {
            System.out.println(next+"==>");
            return "Node{" +
                    "key='" + key + '\'' +
                    ", value=" + value +
                    '}';
        }
    }

    public static void main(String[] args) {
        LearnHash l = new LearnHash();
        l.put("name","张三丰");
        l.put("name","李四");

        System.out.println(Arrays.toString(l.table));

    }

}
